﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
namespace TDLib.Security
{
    /// <summary>
    /// 新版的des加密解密类
    /// </summary>
    public class NewDES
    {

        //加密钥匙
        private static byte[] DEkey = new byte[] { 11, 23, 93, 102, 72, 41, 18, 12 };
        //解密钥匙
        private static byte[] DESIV = new byte[] { 75, 158, 46, 97, 78, 57, 17, 36 };

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="value">待加密的字符串</param>
        /// <returns></returns>
        public string Encode(string value)
        {
            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                using (CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateEncryptor(DEkey, DESIV), CryptoStreamMode.Write))
                {
                    using (StreamWriter objStreamWriter = new StreamWriter(objCryptoStream))
                    {
                        objStreamWriter.Write(value);
                        objStreamWriter.Flush();
                        objCryptoStream.FlushFinalBlock();
                        objMemoryStream.Flush();
                        string encodeValue = Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);
                        objDES.Clear();
                        return encodeValue;
                    }
                }
            }

        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="value">待解密的字符串</param>
        /// <returns></returns>
        public string Decode(string value)
        {
            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            byte[] Input = Convert.FromBase64String(value);
            using (MemoryStream objMemoryStream = new MemoryStream(Input))
            {
                using (CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateDecryptor(DEkey, DESIV), CryptoStreamMode.Read))
                {
                    using (StreamReader objStreamReader = new StreamReader(objCryptoStream))
                    {
                        string decodeValue = objStreamReader.ReadToEnd();
                        objDES.Clear();
                        return decodeValue;
                    }
                }
            }
        }
    }
}
